#include "avatar_db.h"

AvatarDb::AvatarDb(QObject *parent)
    : QObject(parent), is_valid_(false)
{
    db_ = QSqlDatabase::addDatabase("QSQLITE");
    db_.setDatabaseName("avatar.db");

    CheckDbStructure();
}

AvatarDb::~AvatarDb()
{
    if(db_.isOpen())
    {
        qDebug() << "connection closed!!!!!!!!!!!!!!!!!!!!";
        db_.close();
    }
}

bool AvatarDb::GetAllAvatars(QList<Avatar> &result_list)
{
    if(!is_valid_)
    {
        return false;
    }
    else if(db_.isOpen())
    {
        QSqlQuery query;
        bool success = query.exec("SELECT uuid, name, score FROM avatar");

        if(!success)
        {
            qDebug() << "Error creating Db";
            return false;
        }
        else
        {
            while(query.next())
            {
                QString uuid = query.value(0).toString();
                QString name = query.value(1).toString();
                double score = query.value(2).toDouble();
                Avatar avatar(uuid, name, score);

                result_list << avatar;
            }
        }
    }

    return false;
}

bool AvatarDb::SaveAvatar(const Avatar &avatar)
{
    if(!is_valid_)
    {
        return false;
    }
    else if(db_.isOpen())
    {
        QSqlQuery query;
        query.prepare("SELECT uuid FROM avatar WHERE uuid = :uuid");
        query.bindValue(":uuid", avatar.uuid());
        bool success = query.exec();

        if(!success)
        {
            qDebug() << "Error querying Db";
            return false;
        }
        else
        {
            while(query.next())
            {
                return false;
            }

            query.clear();
            query.prepare("INSERT INTO avatar VALUES (:uuid, :name, :score)");
            query.bindValue(":uuid", avatar.uuid());
            query.bindValue(":name", avatar.name());
            query.bindValue(":score", avatar.score());

            success = query.exec();
            if(!success)
            {
                qDebug() << "Insert Db failed";
                return false;
            }
            else
            {
                qDebug() << "insert uuid:"<<avatar.uuid() << " name:" << avatar.name() << " score: "<< avatar.score();
                return true;
            }
        }
    }

    return false;
}

bool AvatarDb::SaveAvatars(const QList<Avatar> &avatars)
{
    //TODO: change to transaction
    for(auto item : avatars)
    {
        SaveAvatar(item);
    }

    return true;
}

bool AvatarDb::DeleteAvatar(const Avatar &avatar)
{
    if(!is_valid_)
    {
        return false;
    }
    else if(db_.isOpen())
    {
        QSqlQuery query;
        query.prepare("DELETE FROM avatar WHERE uuid= :uuid");
        query.bindValue(":uuid", avatar.uuid());

        bool success = query.exec();
        if(!success)
        {
            qDebug() << "delete Db failed";
            return false;
        }
        else
        {
            qDebug() << "delete uuid:"<<avatar.uuid() << " name:" << avatar.name() << " score: "<< avatar.score();
            return true;
        }
    }

    return false;
}

bool AvatarDb::DeleteAvatars(const QList<Avatar> &avatars)
{
    //TODO: change to transaction
    for(auto item : avatars)
    {
        DeleteAvatar(item);
    }

    return true;
}

void AvatarDb::CheckDbStructure()
{
    if (!db_.open())
    {
        qDebug() << "Error: connection with database failed";
        is_valid_ = false;
    }
    else
    {
        QSqlQuery query;
        bool success = query.exec("CREATE TABLE IF NOT EXISTS avatar ("
                                  "uuid VARCHAR(100) PRIMARY KEY, "
                                  "name TEXT NOT NULL, "
                                  "score INTEGER NOT NULL)");

        if(!success)
        {
            qDebug() << "Error creating Db";
            is_valid_ = false;
        }
    }

    is_valid_ = true;
}
